package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.UserRole;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.IRoleService;
import com.zdd.ehe.service.IUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-13
 */
@Api(tags = "用户角色管理模块")
@RestController
@RequestMapping("/userRole")
@Slf4j
public class UserRoleController {
    @Resource
    private IUserRoleService userRoleService;
    @Resource
    private IRoleService roleService;


    @ApiOperation("分页用户角色接口")
    @PostMapping("/getalluserrolebypage")
    public Result getAllUserRoleByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Page<UserRole> pagex = new Page<>(page,pageSize);
        Page<UserRole> pageResult = userRoleService.getAllUserRoleByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }


    @ApiOperation("通过username查询")
    @GetMapping("/getbyusername")
    public Result getByUserName(@RequestParam("userName") String userName){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("userName",userName);
        List<UserRole> userRoleList = userRoleService.getAllByUserName(userName);
        //log.info("userRoleList:" + userRoleList);
        if(userRoleList.size() != 0){
            Map<String,Object> data = new HashMap<>();
            data.put("userRoleList",userRoleList);
            data.put("total",userRoleList.size());
            data.put("size",10);
            data.put("current",1);
            data.put("pages",1);
            return Result.success("查询成功!",data);
        }else{
            return Result.error(4001,"未查询到该用户信息");
        }

    }

    @ApiOperation("删除用户角色接口")
    @PostMapping("/deleteuserrolebyid")
    public Result deleteUserRoleById(@RequestParam("id") int id){
        UserRole userRole = userRoleService.getById(id);
        if(userRole == null){
            return Result.error(4001,"编号错误");
        }
        //0为非启用状态
        userRole.setAuthorizeStatus("0");
        boolean result = userRoleService.updateById(userRole);
        if(result == false){
            return Result.error(4002,"删除失败");
        }
        return Result.success("删除成功");

    }

    @ApiOperation("重新启用用户角色接口")
    @PostMapping("/restartuserrolebyid")
    public Result reStartuserRoleById(@RequestParam("id") int id){
        UserRole userRole = userRoleService.getById(id);
        if(userRole == null){
            return Result.error(4001,"编号错误");
        }
        userRole.setAuthorizeStatus("1");
        boolean result = userRoleService.updateById(userRole);
        if(result == false){
            return Result.error(4002,"重新启用失败");
        }
        return Result.success("重新启用成功");
    }


    @ApiOperation("获取用户角色信息接口")
    @PostMapping("/showuserrole")
    public UserRole showUserRole(@RequestParam("userId") int userId){

        //userId 一定不为空，所以不做判断
        UserRole userRole = userRoleService.getById(userId);
        return userRole;
    }


    @ApiOperation("修改用户角色信息接口")
    @PostMapping("/updateuserrolebyid")
    public Result updateUserRoleById(@RequestBody Map<String ,Object> data){
        //1.删除有 Userid 的行
        //2.新增

        Integer userId = (Integer) data.get("userId");
        List<Integer> roleIds  = (List<Integer>) data.get("roleIds");
        //log.info("roleIds: " + roleIds);

        int result = userRoleService.deleteById(userId);
        if(result != -1){
            //遍历 roleId列表
            for(int i= 0;i<roleIds.size();i++){
                int role_id = roleIds.get(i);
                UserRole userRole = new UserRole();
                userRole.setRoleId(role_id);
                userRole.setUserId(userId);
                userRole.setAuthorizeTime(LocalDateTime.now());
                userRole.setAuthorizeStatus("1");
                int r = userRoleService.insert(userRole);
                if(r == -1){
                    return Result.error("修改失败");
                }
            }

        }
        return Result.success("修改成功");
    }


    @ApiOperation("根据用户id查询对应角色的接口")
    @PostMapping("/getrolebyid")
    public Result getRoleById(@RequestParam("userId") int userId){
        List<Integer> roleIds = userRoleService.getRoleIdByUserId(userId);
        //log.info("roleids: " + roleIds);
        if(roleIds.size() == 0){
            return Result.error(4001,"查询失败");
        }
        return Result.success("查询成功",roleIds);
    }







}
